package com.ltmall.api.controller;

import com.ltmall.api.bean.ClientInfo;
import com.ltmall.api.bean.Goods;
import com.ltmall.api.bean.HotCity;
import com.ltmall.api.bean.ThSku;
import com.ltmall.api.comm.CityDataParse;
import com.ltmall.api.entity.JsonCity;
import com.ltmall.api.manager.AccountManager;
import com.ltmall.api.manager.GoodsManager;
import com.ltmall.api.manager.SearchManager;
import com.ltmall.api.parse.GoodsParse;
import com.ltmall.api.parse.bean.City;
import com.ltmall.api.util.PageUtil;
import com.ltmall.api.util.RemoteUtil;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class SearchController {

    boolean opTag, valiTag;
    private SearchManager searchManager;
    private GoodsManager goodsManager;
    private AccountManager accountManager;

    public AccountManager getAccountManager() {
        return accountManager;
    }

    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }

    public SearchManager getSearchManager() {
        return searchManager;
    }

    public void setSearchManager(SearchManager searchManager) {
        this.searchManager = searchManager;
    }

    public GoodsManager getGoodsManager() {
        return goodsManager;
    }

    public void setGoodsManager(GoodsManager goodsManager) {
        this.goodsManager = goodsManager;
    }

    /**
     * 获取所有的开通的城市
     *
     * @param request
     * @param response
     */
    public void cityList(HttpServletRequest request, HttpServletResponse response) {
        City city = new City();
        try {
            // 获取json文件的路径
            // TODO 原来是 [getServletContext().getRealPath("/")]
            String jsonPath = request.getServletContext().getRealPath("/");
            CityDataParse.initJsonPath(jsonPath);

            String userID = request.getParameter("userID");
            JsonCity homeCity = new JsonCity();
            ClientInfo info = null;
            /*
             * 1、返回籍贯所在的地区 2、籍贯所在地区所属地级市的其他的县市区 3、全国开通城市列表
             */

            // 1,籍贯城市
            if (userID != null && !userID.equals("")) {
                info = accountManager.getInfo(Integer.parseInt(userID));
                if (info != null) {
                    homeCity.setProvince(info.getProvince());
                    homeCity.setCity(info.getCity());
                    homeCity.setCounty(info.getCounty());
                }
            }
            city.setHomeCity(homeCity);

            // RemoteUtil.response(response, city, 1, "返回成功");

            // 2，推荐城市
            if (info != null) {
                if (info.getCounty() != null && !info.getCounty().equals("")) {
                    city.setRecommendCity(CityDataParse.getCounty(info.getCity(), info.getCounty()));
                } else {
                    city.setRecommendCity(CityDataParse.getCitys(info.getProvince(), info.getCity()));
                }
            }

            // RemoteUtil.response(response, city, 1, "返回成功");

            // 全国开通的城市
            List<?> list = searchManager.cityList();
            List<JsonCity> cityList = com.ltmall.api.parse.CityParse
                    .cityListParse(list);
            Set<JsonCity> allCity = new HashSet<JsonCity>();
            allCity.addAll(cityList);

            city.setAllCity(allCity);

            RemoteUtil.response(response, city, 1, "返回成功");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            RemoteUtil.response(response, city, 0, "返回失败");
        } catch (IllegalStateException e) {
            e.printStackTrace();
            RemoteUtil.response(response, city, 1, "返回失败");
        }
    }

    // 城市搜索
    public void citySearch(HttpServletRequest request, HttpServletResponse response) {
        String keyWord = request.getParameter("keyWord");
        List<HotCity> cityList = searchManager.searchCity(keyWord);
        RemoteUtil.response(response, cityList, 1, "返回成功");
    }

    // 商品搜索
    public void goodsSearch(HttpServletRequest request, HttpServletResponse response) {
        String keyword = request.getParameter("keyWord");
        PageUtil page = new PageUtil(Integer.parseInt(request.getParameter("page")));
        List<Goods> goodsList = searchManager.searchGoos(keyword, page);
        for (Goods goods : goodsList) {
            goods.setGoodsPicture(GoodsParse.getOneGoodsPicture(goods.getGoodsPicture()));
        }
        List<Goods> goodsList0 = goodsManager.convertSkuData(goodsList);
        for (Goods goods : goodsList0) {
            int temp = Integer.MAX_VALUE;
            for (ThSku sku : goods.getThSkus()) {
                if (sku.getHidden() == 0 && sku.getID() < temp) {
                    temp = sku.getID();
                    goods.setFakeSellOut(sku.getFakeSellout());
                    goods.setSellOut(sku.getSellout());
                    goods.setGoodsNumber(sku.getStock());
                }
                //董事特权只能享受分润总金额的90%
                if (sku.getProfitShare() >= 0) {
                    int profitShare = (int) ((sku.getProfitShare()) * 0.9);
                    sku.setProfitShare(profitShare);
                }
            }
        }
        RemoteUtil.response(response, goodsList0, 1, "返回成功");
    }

}
